<template>
	<view class="container">
		<view class="form">
			<uni-forms ref="formRef" :model="form" :rules="rules" label-position="top">
				<uni-forms-item label="角色名称" name="roleName" required>
					<uni-easyinput v-model="form.roleName" placeholder="请输入角色名称" />
				</uni-forms-item>
				<uni-forms-item label="权限字符" name="roleKey" required>
					<uni-easyinput v-model="form.roleKey" placeholder="请输入权限字符" />
				</uni-forms-item>
				<uni-forms-item label="角色顺序" name="roleSort" required>
					<uni-easyinput v-model="form.roleSort" placeholder="请输入角色顺序" />
				</uni-forms-item>
				<uni-forms-item label="状态" name="status" required>
					<xy-switch v-model="form.status" active-value="0" inactive-value="1" />
				</uni-forms-item>
				<uni-forms-item label="菜单权限" name="menuIds" required>
					<xy-drawer-tree-select v-model="form.menuIds" :data="menuOptions" value-name="id" multiple />
				</uni-forms-item>
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="form.remark" placeholder="请输入备注" />
				</uni-forms-item>
				<uni-forms-item>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="submitForm()">确认</button>
					<button class="mini-btn xy-btn" type="warn" size="mini" @click="cancel()">取消</button>
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad,
		onUnload
	} from '@dcloudio/uni-app'
	import {
		addRole,
		changeRoleStatus,
		dataScope,
		delRole,
		getRole,
		listRole,
		updateRole,
		deptTreeSelect
	} from "@/pages_work/api/system/role";
	import {
		roleMenuTreeselect,
		treeselect as menuTreeselect
	} from "@/pages_work/api/system/menu";
	const {
		proxy
	} = getCurrentInstance();
	const {
		sys_normal_disable
	} = proxy.useDict("sys_normal_disable");

	/** 表单数据 */
	const form = ref({});

	const menuOptions = ref([]);

	/** 表单检验规则 */
	const rules = ref(proxy.uniappRules({
		roleName: [{
			required: true,
			message: "角色名称不能为空",
			trigger: "blur"
		}],
		roleKey: [{
			required: true,
			message: "权限字符不能为空",
			trigger: "blur"
		}],
		roleSort: [{
			required: true,
			message: "角色顺序不能为空",
			trigger: "blur"
		}]
	}))

	onLoad((params) => {
		uni.setNavigationBarTitle({
			title: params.title
		});
		if (params.roleId == undefined || params.roleId == null || params.roleId == '') {
			getMenuTreeselect();
		} else {
			handleUpdate(params);
		}
	})

	/** 取消按钮 */
	const cancel = () => {
		uni.navigateBack()
	}

	/** 提交按钮 */
	const submitForm = () => {
		proxy.$refs["formRef"].validate().then(valid => {
			if (valid) {
				if (form.value.roleId != undefined) {
					updateRole(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						setTimeout(uni.navigateBack, 1000);
					});
				} else {
					addRole(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						setTimeout(uni.navigateBack, 1000);
					});
				}
			}
		});
	};
	
	/** 查询菜单树结构 */
	const getMenuTreeselect = () => {
		menuTreeselect().then(response => {
			menuOptions.value = response.data;
		});
	}
	
	/** 所有部门节点数据 */
	const getDeptAllCheckedKeys = () => {
		// // 目前被选中的部门节点
		// let checkedKeys = deptRef.value.getCheckedKeys();
		// // 半选中的部门节点
		// let halfCheckedKeys = deptRef.value.getHalfCheckedKeys();
		// checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
		// return checkedKeys;
	}
	
	/** 修改角色 */
	const handleUpdate = (row) => {
		const roleId = row.roleId;
		getRole(roleId).then(response => {
			form.value = response.data;
			form.value.roleSort = Number(form.value.roleSort);
			getRoleMenuTreeselect(roleId);
		});
	}
	
	/** 根据角色ID查询菜单树结构 */
	const getRoleMenuTreeselect = (roleId) => {
		roleMenuTreeselect(roleId).then(response => {
			menuOptions.value = response.menus;
			form.value.menuIds = response.checkedKeys
		});
	}
	
	onUnload(() => {
		uni.$emit('update')
	})
	
</script>

<style lang="scss">
	@import "@/static/scss/work/form.scss";
</style>